<template>
	<view class="scrool-page">
		<view class="list-cont">

			<scroll-view class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<!-- <view v-for="(item,index) in productList" :key="index">
					<uni-card v-if="item.attr9 == 'true'" @click="detail(item)" style='background-color: darkgrey;'>
						<text class="uni-body">货位：{{ item.areaCode }}</text><br>						
						<text class="uni-body" v-if="item.attr9 == 'true'">状态：已完成</text>
						<text class="uni-body" v-else>状态：未完成</text>
					</uni-card>
					
					<uni-card v-else @click="detail(item)">
						<text class="uni-body">货位：{{ item.areaCode }}</text><br>
						<text class="uni-body" v-if="item.attr9 == 'true'">状态：已完成</text>
						<text class="uni-body" v-else>状态：未完成</text>
					</uni-card>
				</view> -->
				<uni-row :gutter="0">
					<view v-for="(item,index) in productList" :key="index">
						<view>
							<!-- <uni-row :gutter="0"> -->
							<uni-col :span="12">
								<uni-card :style="{backgroundColor:item.isCheck == 'Y'?'darkgrey':'#fff'}"
									@click="detail(item)">
									<text class="uni-body">货位：{{ item.areaCode }}</text><br>
									<text class="uni-body" v-if="item.isCheck == 'Y'">状态：已完成</text>
									<text class="uni-body" v-else>状态：未完成</text>
								</uni-card>


								<!-- <uni-card v-if="item.attr9 == '1' || item.attr9 == '2' || status =='3'"
									@click="detail(item)" style='background-color: darkgrey;'>
									<text class="uni-body">货位：{{ item.areaCode }}</text><br>
									<text class="uni-body"
										v-if="item.attr9 == '1' || item.attr9 == '2' || status =='3'">状态：已完成</text>
									<text class="uni-body" v-else>状态：未完成</text>
								</uni-card>

								<uni-card v-else @click="detail(item)">
									<text class="uni-body">货位：{{ item.areaCode }}</text><br>
									<text class="uni-body"
										v-if="item.attr9 == '1' || item.attr9 == '2' || status =='3'">状态：已完成</text>
									<text class="uni-body" v-else>状态：未完成</text>
								</uni-card> -->
							</uni-col>
							<!-- </uni-row>	 -->
						</view>
						<!-- <view v-else>
							<uni-col :span="12">
								<uni-card v-if="item.attr9 == '1' || item.attr9 == '2' || status =='3'"
									@click="detail(item)" style='background-color: darkgrey;'>
									<text class="uni-body">货位：{{ item.areaCode }}</text><br>
									<text class="uni-body"
										v-if="item.attr9 == '1' || item.attr9 == '2' || status =='3'">状态：已完成</text>
									<text class="uni-body" v-else>状态：未完成</text>
								</uni-card>

								<uni-card v-else @click="detail(item)">
									<text class="uni-body">货位：{{ item.areaCode }}</text><br>
									<text class="uni-body"
										v-if="item.attr9 == '1' || item.attr9 == '2' || status =='3'">状态：已完成</text>
									<text class="uni-body" v-else>状态：未完成</text>
								</uni-card>
							</uni-col>
						</view> -->
					</view>
				</uni-row>
			</scroll-view>
		</view>

		<view class="footer">

			<button v-if="this.status == 0 || 	this.status ==1" type="default" size='mini'
				style="width:90%;height: 35px; line-height: 35px ;" @click="submit">完成</button>

			<!-- <button type="default" size='mini' style="width:48%; margin-left:1%;" @click="save">暂存</button> -->

		</view>

		<uni-popup ref="inputDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog mode="input" :title="alertTitle" :value='dataValue' :before-close="true"
				@confirm="alertConfirm" @close="alertClose">
			</uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="alertDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true"
				:content="content" @confirm="dialogConfirm" @close="dialogClose">
			</uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="dataDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true"
				content="检测到有缓存数据是否加载？" @confirm="dataConfirm" @close="dataClose">
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		InventorySheetSubmission
	} from '@/api/system/interfaceFile.js'
	import {
		getToken
	} from '@/utils/auth'
	import {
		debounce
	} from '@/api/system/Tool.js'
	export default {
		data() {
			return {
				materialData: '',
				itemIds: null,
				docType: null,
				productList: [],
				baseUrl: getApp().globalData.config.baseUrl,
				userName: this.$store.state.user.name,
				orgId: getApp().globalData.config.orgId,
				pkOrder: '',
				status: '',
				title: '',
				id: '',
				index: '',
				alertTitle: '',
				dataValue: '',
				record: {},
				content: '物料账实不符，请重新扫箱码',
				exceptionList: [],
				sumbitFlag: false,
			}
		},

		mounted() {
			// const value = uni.getStorageSync('wms_check_order' + this.pkOrder);
			// if (value.length > 0) {
			// 	this.$refs.dataDialog.open();
			// } else {
			// 	this.query();
			// }
		},
		onReady() {
			// const value = uni.getStorageSync('wms_check_order' + this.pkOrder);
			// console.log('value', value)
			// if (value.length > 0) {
			// 	this.$refs.dataDialog.open();
			// } else {
			// 	this.query();
			// }
			// this.query();
		},
		onShow() {
			this.query();
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			this.orgId = uni.getStorageSync('orgId');


			this.pkOrder = option.pkOrder;
			this.status = option.status;
			this.title = option.title;
			this.id = option.id;
			this.index = option.index;
			console.log('	this.tasktype', this.tasktype, uni.getStorageSync('orgId'))
			this.tasktype = option.tasktype

			uni.setNavigationBarTitle({
				title: this.title
			});


		},

		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
				return false;
			}
			if (!that.sumbitFlag) {
				try {
					uni.setStorageSync('wms_check_order' + that.pkOrder, that.productList);
					uni.setStorageSync('wms_check_order_exception' + that.pkOrder, that.exceptionList);
				} catch (e) {
					console.log(e.message);
				}
			}
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			let nowPage = pages[pages.length - 1]; //当前页页面实例
			let prevPage = pages[pages.length - 2];
			prevPage.$vm.chooseTab(that.index);
			setTimeout(function() {
				uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
			}, 1000);
			return true;
		},

		methods: {
			query() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this;

				if (that.pkOrder == undefined) {
					that.pkOrder = uni.getStorageSync('pkOrder')
				}
				console.log('query', that.orgId, that.userName, that.pkOrder, );
				uni.request({
					url: that.baseUrl + '/system/WmsCheckOrder/areacodes1',
					data: {
						orgId: that.orgId,
						userName: that.userName,
						pkOrder: that.pkOrder
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {


						//隐藏加载框
						const {
							data
						} = res.data

						console.log('query', data);
						uni.setStorageSync('materialData', data.plan.itemIds)
						if (res.data.code == '200') {
							this.docType = data.plan.docType
							that.itemIds = data.plan.itemIds
							this.productList = data.lineList;
							console.log(this.productList, 'this.productList')
							//隐藏加载框
							uni.hideLoading();
						} else {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return;
						}

					},
				})
			},


			detail(item) {
				var that = this;
				if (item.isCheck == 'Y') {
					uni.showToast({
						title: '任务已完成',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				if (that.status == '2') {
					uni.showToast({
						title: '任务已挂起，请先恢复',
						icon: 'none',
						duration: 3000
					});
					return;
				}

				if (that.status == '3') {
					uni.showToast({
						title: '任务已完成，不能修改',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				that.alertTitle = "请输入" + item.areaCode + "库位的箱数";
				that.record = item;
				uni.setStorageSync('storageLocation', this.record.areaCode)
				uni.setStorageSync('pkOrder', this.pkOrder)

				if (this.tasktype == '初盘') {
					// that.$refs.inputDialog.open();
					uni.navigateTo({
						url: `/pages/special/wms_check_order_detaile?docType=${this.docType}&itemIds=${this.itemIds}&storageLocation=${that.record.areaCode}&batchNumber=${that.record.boxCount}&pkOrder=${ that.pkOrder}&id=${that.id}&status=${that.status}&areaId=${that.record.areaId}&tasktype=${that.tasktype}`


						// url: '/pages/special/wms_check_order_detaile?itemIds=`${}`storageLocation=' + that.record.areaCode +
						// 	"&batchNumber=" + that.record.boxCount+"&pkOrder=" + that.pkOrder+"&id="+that.id+"&status="+that.status+'&areaId='+  that.record.areaId+'&this.tasktype='+that.tasktype
					})

				} else {
					console.log('this.itemIds', item)
					uni.navigateTo({
						url: '/pages/special/wms_check_order_item_detaile?pkOrder=' + this.pkOrder +
							'&storageLocation=' + that.record.areaCode + "&batchNumber=" + that.record.boxCount +
							'&areaId=' + that.record.areaId +
							'&tasktype=' + that.tasktype + '&itemIds=' + this.itemIds + '&docType=' + this.docType
					})
				}

			},

			save() {
				var that = this;
				if (that.status == '2') {
					uni.showToast({
						title: '任务已挂起，请先恢复',
						icon: 'none',
						duration: 3000
					});
					return;
				}

				if (that.status == '3') {
					uni.showToast({
						title: '任务已完成，不能修改',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				if (!that.sumbitFlag) {
					try {
						uni.setStorageSync('wms_check_order' + that.pkOrder, that.productList);
						uni.setStorageSync('wms_check_order_exception' + that.pkOrder, that.exceptionList);
					} catch (e) {
						console.log(e.message);
					}
				}
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2];
				prevPage.$vm.chooseTab(that.index);
				setTimeout(function() {
					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
						delta: 1
					});
				}, 1000);
				//隐藏加载框
				uni.hideLoading();
			},

			submit() {

				debounce(() => {

					let productListData = uni.getStorageSync('ScanCodeData')
					console.log(this.baseUrl, 'this.baseUrl')
					console.log('productList', this.productList, this.productList.every(item => item.isCheck !=
						'Y'))
					if (!this.productList.every(item => item.isCheck == 'Y')) {
						uni.showToast({
							title: '盘点单有未完成的状态，请检查后重新提交',
							icon: 'none',
							duration: 3000
						});
						return;
					}
					uni.showLoading({
						title: '提交中',
						mask: true
					});




					let params = {
						orgId: this.orgId,
						userName: this.userName,
						pkOrder: this.pkOrder,

					}

					InventorySheetSubmission('/system/WmsCheckOrder/checkLine/submitReport', params).then(
						res => {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 3000
							});
							uni.hideLoading();


							uni.switchTab({
								url: '/pages/work/index'
							})

						}).catch(err => {
						uni.showToast({
							title: err,
							icon: 'none',
							duration: 3000
						});
						uni.hideLoading();
						console.log(err, 'res')

					})
				}, 1000)

				// var that = this;
				// let exceptionListVALUE = uni.getStorageSync('exceptionListVALUE')

				// console.log('productListData', productListData)
				// console.log('this.productList', this.productList)
				// console.log(this.productList,'productList')
				// if (that.status == '2') {
				// 	uni.showToast({
				// 		title: '任务已挂起，请先恢复',
				// 		icon: 'none',
				// 		duration: 3000
				// 	});
				// 	return;
				// }

				// if (that.status == '3') {
				// 	uni.showToast({
				// 		title: '任务已完成，不能修改',
				// 		icon: 'none',
				// 		duration: 3000
				// 	});
				// 	return;
				// }
				// uni.showLoading({
				// 	title: '加载中',
				// 	mask: true
				// });
				// var count = 0;
				// var tempSuccessList = [];

				// for (var i = 0; i < that.productList.length; i++) {
				// 	if (that.productList[i].attr9 == '1' || that.productList[i].attr9 == '2') {
				// 		count++;
				// 	}
				// 	if (that.productList[i].attr9 == '1') {
				// 		tempSuccessList.push(that.productList[i]);
				// 	}
				// }
				// // count == that.productList.length
				// if (true) {
				// 	uni.setStorageSync('exceptionListVALUE', false)
				// 	uni.request({
				// 		url: that.baseUrl + '/system/WmsCheckOrder/checkLine/reportCheckLineTrueAndFalse',
				// 		data: {
				// 			orgId: that.orgId,
				// 			userName: that.userName,
				// 			pkOrder: that.pkOrder,
				// 			successList: tempSuccessList,
				// 			exceptionList: that.exceptionList
				// 		},
				// 		method: 'POST',
				// 		header: {
				// 			'authorization': getToken()
				// 		},
				// 		dataType: 'json',
				// 		success: (res) => {
				// 			//隐藏加载框
				// 			uni.hideLoading();

				// 			that.sumbitFlag = true;
				// 			console.log(res.data);
				// 			if (res.data.code == '200' || res.data.code == 200) {
				// 				that.updateTask();
				// 				//隐藏加载框
				// 				uni.hideLoading();
				// 			} else {
				// 				//隐藏加载框
				// 				uni.hideLoading();
				// 				uni.showToast({
				// 					title: res.data.msg,
				// 					icon: 'none',
				// 					duration: 5000
				// 				});
				// 				return;
				// 			}

				// 		},
				// 		error: (err) => {

				// 		}
				// 	})
				// } else {
				// 	//隐藏加载框
				// 	uni.hideLoading();
				// 	uni.showModal({
				// 		title: '提示',
				// 		content: '此盘点单尚未完成，是否临时退出？',
				// 		success: function(res) {
				// 			if (res.confirm) {
				// 				if (!that.sumbitFlag) {
				// 					try {
				// 						uni.setStorageSync('wms_check_order' + that.pkOrder, that.productList);
				// 						uni.setStorageSync('wms_check_order_exception' + that.pkOrder, that
				// 							.exceptionList);
				// 					} catch (e) {
				// 						console.log(e.message);
				// 					}
				// 				}
				// 				let pages = getCurrentPages(); //获取所有页面栈实例列表
				// 				let nowPage = pages[pages.length - 1]; //当前页页面实例
				// 				let prevPage = pages[pages.length - 2];
				// 				prevPage.$vm.chooseTab(that.index);
				// 				setTimeout(function() {
				// 					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
				// 						delta: 1
				// 					});
				// 				}, 1000);
				// 			} else {
				// 				return;
				// 			}
				// 		}
				// 	})
				// }
			},

			updateTask() {
				var that = this;
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request({
					url: that.baseUrl + '/system/wmsbacklog/update',
					data: {
						id: that.id,
						executor: that.userName,
						orgId: that.orgId,
						startTime: '',
						endTime: new Date().toISOString().slice(0, 10),
						status: '3' //完成状态
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							uni.showToast({
								title: '盘点成功并且代办任务已完成',
								icon: 'none',
								duration: 3000
							});
							setTimeout(function() {
								let pages = getCurrentPages(); //获取所有页面栈实例列表
								let nowPage = pages[pages.length - 1]; //当前页页面实例
								let prevPage = pages[pages.length - 2];
								prevPage.$vm.chooseTab(that.index);
								uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							}, 1000);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}

					},
				})
			},

			alertConfirm(value) {
				var that = this;
				if (that.record.boxCount == value) {
					that.record.qtyCheck = value;
					that.record.attr9 = "1";
					that.$refs.inputDialog.close();
				} else {
					that.record.qtyCheck = value;
					that.$refs.alertDialog.open();
				}
			},

			alertClose() {
				this.$refs.inputDialog.close();
			},

			//点击确定跳转到全盘扫描界面
			dialogConfirm() {
				var that = this;
				uni.navigateTo({
					url: '/pages/special/wms_check_order_item_detaile?storageLocation=' + that.record
						.areaCode +
						"&batchNumber=" + that.record.boxCount
				})
				that.$refs.inputDialog.close();
				that.$refs.alertDialog.close();
			},

			dialogClose() {
				//this.$refs.inputDialog.close();
				this.$refs.alertDialog.close();
			},

			dataConfirm() {
				try {
					const value = uni.getStorageSync('wms_check_order' + this.pkOrder);
					this.productList = value;
					const exceptiomValue = uni.getStorageSync('wms_check_order_exception' + this.pkOrder);
					this.exceptionList = exceptiomValue;
					uni.removeStorageSync('wms_check_order' + this.pkOrder);
					uni.removeStorageSync('wms_check_order_exception' + this.pkOrder);
				} catch (e) {
					console.log(e.message);
				}
				this.$refs.dataDialog.close();
			},

			dataClose() {
				uni.removeStorageSync('wms_check_order' + this.pkOrder);
				uni.removeStorageSync('wms_check_order_exception' + this.pkOrder);
				this.$refs.dataDialog.close();
				this.query();
			},
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}

	.text-content {
		font-size: 48rpx;
		text-align: center;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}

	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: 30px;
		font-size: 26rpx;
	}

	page {
		width: 100%;
		height: 100%;
	}


	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}

	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}

	.list-cont {
		flex: 1;
		position: relative;

		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}

		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;

		}
	}


	.footer {
		color: #fff;
		text-align: center;
		width: 100%;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>